
from direct.fsm import FSM
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import *
from pandac.PandaModules import *

class Character(FSM.FSM):
  def __init__(self,parent):
    FSM.FSM.__init__(self,"Character")
    
    self.nextState = {
      ('Welcome', 'idle') : 'Idle',
      ('Idle', 'proceed') : 'Question',
      ('Question', 'correct') : 'CorrectAnswer',
      ('Question', 'wrong') : 'WrongAnswer',
      ('CorrectAnswer', 'proceed') : 'Question',
      ('CorrectAnswer', 'won') : 'GameWon',
      ('WrongAnswer', 'proceed') : 'Question',
      ('WrongAnswer', 'lost') : 'GameLost'
  }

    
    self.char = Actor('actors/gw01.egg',{
      'wave1':'actors/gw02.egg'
      ,'wave2':'actors/gw03.egg'
      ,'wave3':'actors/gw04.egg'
      ,'wave4':'actors/gw05.egg'
      ,'wave5':'actors/gw06.egg'
      ,'wave6':'actors/gw07.egg'
      ,'wave7':'actors/gw08.egg'
        })
      
    self.mover = parent.attachNewNode("mover")
    self.mover.setPos(35,-100,20)
   
    
    self.char.reparentTo(self.mover)
    self.char.setScale(0.05)
    self.char.setH(180)
    self.mover.lookAt(base.camera)
      
    
    #self.char.setTransparency(TransparencyAttrib.MAlpha)
    
    x=self.mover.getX()
    y=self.mover.getY()
    z=self.mover.getZ()
    
    moveUp = LerpPosInterval(self.mover, duration=2.0, pos=Point3(x,y,z+2), startPos = Point3(x,y,z-2), 
                other = None, blendType = 'easeInOut', 
                bakeInStart = 1, fluid = 0, name = 'mover')
                
    moveDown = LerpPosInterval(self.mover, duration=2.0, pos=Point3(x,y,z-2), startPos = Point3(x,y,z+2), 
                other = None, blendType = 'easeInOut', 
                bakeInStart = 1, fluid = 0, name = 'mover')
                
    self.moveInCircle = LerpHprInterval(self.mover, duration=2.0, hpr=VBase3(self.mover.getH()+360,0,0),# startHpr=self.mover.getH(), 
                other = None, blendType = 'easeInOut', 
                bakeInStart = 1, fluid = 0, name = 'mover')
                
    self.moveleft = LerpPosInterval(self.mover, duration=0.05, pos=Point3(self.mover.getX()-2,self.mover.getY(),self.mover.getZ()),# startPos = Point3(x,y,z-2), 
                other = None, blendType = 'noBlend', 
                bakeInStart = 1, fluid = 0, name = 'mover')
    
    self.moveright= LerpPosInterval(self.mover, duration=0.05, pos=Point3(self.mover.getX()+2,self.mover.getY(),self.mover.getZ()),# startPos = Point3(x,y,z-2), 
                other = None, blendType = 'noBlend', 
                bakeInStart = 1, fluid = 0, name = 'mover')
                
    self.flyleft= LerpPosInterval(self.mover, duration=3, pos=Point3(self.mover.getX()-50,self.mover.getY(),self.mover.getZ()),# startPos = Point3(x,y,z-2), 
                other = None, blendType = 'noBlend', 
                bakeInStart = 1, fluid = 0, name = 'mover')
                
                
    self.flyright= LerpPosInterval(self.mover, duration=3, pos=Point3(self.mover.getX(),self.mover.getY(),self.mover.getZ()),# startPos = Point3(x,y,z-2), 
                other = None, blendType = 'noBlend', 
                bakeInStart = 1, fluid = 0, name = 'mover')
                
    Sequence(moveUp,moveDown).loop()

    self.accept('idle', self.choice_handler, ['idle'])
    self.accept('proceed', self.choice_handler, ['proceed'])
    self.accept('correct', self.choice_handler, ['correct'])
    self.accept('wrong', self.choice_handler, ['wrong'])
    self.accept('won', self.choice_handler, ['won'])
    self.accept('lost', self.choice_handler, ['lost'])
    
    
    self.request('Welcome')
    

  def choice_handler(self, arg):
    self.request(arg)

  def defaultFilter(self, request, args):
    key = (self.state, request)
    return self.nextState.get(key)
  
  
  def enterWelcome(self):
    #print "char enters welcome state"
    self.char.loop('wave5')
    
  def enterIdle(self):
    #print "char enters idle state"
    self.char.loop('wave1')
    
  def enterQuestion(self):
    #print "char enters question state"
    self.char.loop('wave7')
    
  def enterCorrectAnswer(self):
    #print "char enters correct answer state"
    self.char.play('wave3')
    Sequence(self.moveInCircle).start()
    
    
  def enterWrongAnswer(self):
    #print "char enters wrong answer state"
    self.char.play('wave6')
    Sequence(self.moveleft,self.moveright
    ,self.moveleft,self.moveright
    ,self.moveleft,self.moveright
    ,self.moveleft,self.moveright
    ,self.moveleft,self.moveright
    ,self.moveleft,self.moveright
    ,self.moveleft,self.moveright
    ).start()
    
  def enterGameWon(self):
    #print "char enters Game Won!"
    self.char.loop('wave3')
    self.moveInCircle.duration = 0.1
    Sequence(self.moveInCircle).loop()
    Sequence(self.flyleft, self.flyright).loop()
  
  def enterGameLost(self):
    #print "char enters Game Over!"
    Sequence(Func(self.char.setH, 45), Func(self.char.play, 'wave6')).start()
    